//
// Created by Administrator on 2019/12/10.
//
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>

using namespace std;

class Box {
public:
    double length;
    double width;
    double height;

    /**
     * 获取体积
     * @return
     */
    double getVolume(void);

    void print() {
        cout << "length:" << length << ", width:" << width << ", height:" << height << ", color = " << color << endl;
    }

    void setColor(char *c) {
        color = c;
    }

private:
    char *color;


};

/**
 * 可以在类的外部实现方法
 * @return
 */
double Box::getVolume() {
    return length * width * height;
}


// 打印类相关的数据
void print1() {
    cout << "print1 ----------------" << endl;
    Box box;
    box.length = 5;
    box.width = 6;
    box.height = 7;
    char* color = "yellow";
    box.setColor(color);

    box.print();

    cout << "当前box体积为：" << box.getVolume() << endl;

}


class Shoes {
private:
    char *color;

protected:
    int size;

public:
    char *type;

    void setColor(char *c) {
        color = c;
    }

    void setSize(int size) {
        this->size = size;
    }

    void setType(char *t) {
        type = t;
    }

    Shoes() {
        cout << "Shoes() 无参数构造函数" << endl;
    }

    Shoes(char *type, int size) : type(type), size(size) {
        cout << "Shoes(type, size) 有参数构造函数" << endl;
    }

    ~Shoes() {
        cout << "析构函数Shoes" << ", type:" << type << endl;
    }

    void print() {
        cout << "color:" << color << ", type:" << type << ", size:" << size << endl;
    }

};


void print2() {
    cout << "print2 ----------------" << endl;

    Shoes shoes;
    Shoes *shoes1 = new Shoes("冬款", 23);
    shoes1->setColor("red");

    shoes.setColor("yellow");
    shoes.setSize(24);
    shoes.setType("凉鞋");

    shoes.print();
    shoes1->print();

    delete (shoes1); // 手动释放


    cout << "完毕<<<<<<<<" << endl;
}

class Test1;

void del(Test1 *test1) {
    delete (test1);
}

class Test1 {
private:
    int i;
public:
    Test1() {
        cout << "Test1构造函数" << endl;
    }

    Test1(int i) : i(i) {
        cout << "Test1构造函数" << i << endl;
    }

    ~Test1() {
        cout << "~Test1析构函数" << i << endl;
    }


};

class Test2 {
public:
    Test2() {
        cout << "Test2构造函数" << endl;
    }

    ~Test2() {
        cout << "~Test2析构函数" << endl;
    }

};

void del2(Test2 *test2) {
    delete (test2);
}

void overReloadFunc(int i) {
    cout << "overReloadFunc(int i)" << i << endl;
}

void overReloadFunc(double d) {
    cout << "overReloadFunc(double d)" << d << endl;
}


// 析构函数，构造函数
void print3() {
    cout << "print3 ----------------" << endl;
    Test1 *obj1 = new Test1();
    del(obj1);

    Test2 *obj2 = new Test2();
    del2(obj2);

    clog << "为什么Test1的析构函数没有执行" << endl;

    overReloadFunc(10);
    overReloadFunc(333.3);

    Test1 obj3(20); // 这种方式又会自动执行析构函数
    Test1 &obj4 = obj3;


}

class Demo1 {
private:
    int i;
    int age = 88;
    double score;
public:
    Demo1() {
        cout << "Demo无惨构造" << i << "--" << age << endl;
    }

    Demo1(int i, int b) : i(i), age(b) {
        cout << "Demo1(int i, int b):i(i),age(age)" << i << "--" << age << endl;
    }

    Demo1(double score, int age) : score(56.7), age(40), i(10) {
        cout << "Demo1(double score, int age):score(56.7),age(40)" << score << "--" << age << "--" << i << endl;
    }

    void setDefaultAge(int age1) {
        this->age = age1;
    }

};

void print4() {
    cout << "print4 ----------------" << endl;
    Demo1 demo1, demo2(11, 22), demo3(66.6, 77);


}

// 友元函数测试


// 多态 demo

// 形状类
class Shape {
protected:
    int width, height;

public:
    Shape(int a = 0, int b = 0) {
        this->width = a;
        this->height = b;
        cout << "Shape(int a = 0, int b = 0)" << width << "," << height << endl;
    }

    ~Shape() {
        cout << "~Shape" << width << "," << height << endl;
    }

    int area() {
        cout << "Shape area:" << endl;
        return 0;
    }

    virtual int area2() {
        cout << "virtual Shape area2:" << endl;
        return 0;
    }

};

/**
 * 矩形类
 */
class Rectangle : public Shape {

public:
    Rectangle(int a, int b) : Shape(a, b) {
        cout << "Rectangle(int a = 0, int b = 0)" << a << "," << b << endl;
    }

    ~Rectangle() {
        cout << "~Rectangle" << width << "," << height << endl;
    }

    int area() {
        cout << "Rectangle area:" << endl;
        return width * height;
    }

    int area2() {
        cout << "virtual Rectangle area2:" << endl;
        return width * height;
    }
};

/**
 * 三角形类
 */
class Triangle : public Shape {
public:
    Triangle(int a, int b) : Shape(a, b) {

        cout << "Triangle(int a = 0, int b = 0)" << a << "," << b << endl;
    }

    ~Triangle() {
        cout << "~Triangle" << width << "," << height << endl;
    }

    int area() {
        cout << "Triangle area:" << endl;
        return width * height << 1;
    }

    int area2() {
        cout << "virtual Triangle area2:" << endl;
        return width * height << 1;
    }

};


// 多态demo
void print5() {
    cout << "print5 ----------------" << endl;

    Shape *shape;
    Rectangle rec(10, 7), rec2(100, 70);
    Triangle tri(8, 5), tri2(80, 50);

    shape = &rec;
    shape->area();      // 这个是访问父类的方法

    shape = &tri;
    tri.area();         // 调用子类

    shape->area2();        // 调用子类;

    cout << "下面是直接通过实体来调用---------" << endl;
    rec2.area();
    tri2.area();

    // 下面几个没有调用析构函数
    Rectangle *rectangle1 = new Rectangle(3, 5);
    new Rectangle(8, 58);



}

/**
 * 文件相关操作
 */
void print6() {
    cout << "print6 ----------------" << endl;
    const string fileName = "data.txt";
    char indata[100];
    ofstream outfile;
    outfile.open(fileName);
    outfile << "以下是我个人信息" << endl;
    cout << "请输入你的名字:";
    cin.getline(indata, 100);
    outfile << indata << endl;

    cout << "请输入你的年龄:";
    cin >> indata;
    cin.ignore();       // 不知道这个是啥意思

    outfile << indata << endl;
    // 关闭文件
    outfile.close();

    ifstream infile;
    infile.open(fileName);
    cout << "从" << fileName << "读取文件内容:" << endl;
    infile >> indata;
    cout << indata << endl;
    infile >> indata;
    cout << indata << endl;
    infile >> indata;
    cout << indata << endl;
    infile >> indata;
    cout << indata << endl;
    infile.close();
}

// 文件的批量读写操作
void print7() {

    // https://blog.csdn.net/qq_34097715/article/details/79970860
    cout << "print7 ----------------" << endl;

    string temp;
    ifstream ifs;
    ifs.open("README.md");

    if (!ifs.is_open()) {
        cout << "未成功打开文件" << endl;
    }
    while (getline(ifs, temp)) {
        cout << temp;
        cout << "dddd";
    }

    ifs.close();







//    ofstream ofs;
//    ofs.open("README.md");
//    char *line;
//
//    ofs >> line



}













